﻿<!DOCTYPE html>
<html>
<head>
    <title>获取OpenId</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script type="text/javascript">
        function GetQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
    </script>
</head>
<body>
    <p>code : <label id="lblCode"></label></p>
    <p>OpenId : <label id="lblOpenId"></label></p>
    <p>昵称 : <label id="lblNickName"></label></p>
    <p>头像Url : <img id="imgHeadImgUrl"/></p>
    <script src="http://cdn.bootcss.com/zepto/1.1.4/zepto.min.js" type="text/javascript"></script>
    <script src="asset/js/utils.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var code = GetQueryString("code");
            $("#lblCode").html(code);

            $.ajax({
                url: "api/accesstoken/?code=" + code,
                dataType: 'application/json',
                type: 'GET',
                error: function (data) {
                    $("#lblOpenId").html("fail : " + data.toString());
                },
                success: function (data) {
                    var userInfo = JSON.parse(data);
                    $("#lblOpenId").html(userInfo.openid);
                    $("#lblNickName").html(userInfo.nickname);
                    $("#imgHeadImgUrl").attr("src",userInfo.headimgurl);
                    setCookie("myOpenId", openid, 30);
                    setCookie("myNickName", openid, 30);
                }
            });


        });
    </script>
</body>
</html>
